package com.jxc.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;
import java.util.regex.Pattern;

import org.springframework.util.StringUtils;

import com.google.common.collect.Sets;

public class DateUtils {
	public static final String TIME_STAMP_FORMAT = "yyMMddHHmmssSSS";
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String YYYYMMDD = "yyyyMMdd";
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYY_MM = "yyyy-MM";
	private final static Set<String> formatPattern = Sets.newHashSet();
	static{
		formatPattern.add(TIME_STAMP_FORMAT);
		formatPattern.add(DATE_TIME_FORMAT);
		formatPattern.add(YYYYMMDD);
		formatPattern.add(YYYY_MM_DD);
		formatPattern.add(YYYY_MM);
	}

	public final static String format(Date date,String pattern){
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	/**
	 * 
	 * @param today
	 * @return yyyy-MM-dd 00:00:00.000
	 */
	public final static Date getDayStartTime(Date date) {
		Calendar cal = getDateCalendar(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	/**
	 * 
	 * @param date
	 * @return yyyy-MM-dd 23:59:59.999
	 */
	public final static Date getDayEndTime(Date date) {
		Calendar cal = getDateCalendar(date);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE,59);
		cal.set(Calendar.SECOND, 59);
		cal.set(Calendar.MILLISECOND, 999);
		return cal.getTime();
	}
	
	public final static Date addDay(Date date,int day){
		Calendar cal = getDateCalendar(date);
		cal.add(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}
	
	public final static Date addMonth(Date date,int month){
		Calendar cal = getDateCalendar(date);
		cal.add(Calendar.MONTH, month);
		return cal.getTime();
	}
	
	public final static Date addYear(Date date,int year){
		Calendar cal = getDateCalendar(date);
		cal.add(Calendar.YEAR, year);
		return cal.getTime();
	}
	

	public final static Calendar getDateCalendar(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		return cal;
	}
	
	/**
	    * 上月第一天
	    * @return
	    */
	   public final static Date lastMonthBegin(){
		   Date nowdate = new Date();     
		   Calendar cal = Calendar.getInstance();    
		   cal.setTime(nowdate);
		   cal.add(Calendar.MONTH, -1);       
		  cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH)); 
		  return cal.getTime();
	   }

	   /**
	    * 上月最后一天
	    * @return
	    */
	   public final static Date lastMonthEnd(){
		   Date nowdate = new Date();     
		   Calendar cal = Calendar.getInstance();    
		   cal.setTime(nowdate);
		   cal.add(Calendar.MONTH, -1);       
		   cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH)); 
		  return cal.getTime();
	   }
	   
	   /**
	    * 当前月第一天
	    * @return
	    */
	   public static Date currentDateBegin(){
		   Date nowdate = new Date();     
		   Calendar cal = Calendar.getInstance();    
		   cal.setTime(nowdate);
		   cal.add(Calendar.MONTH,0);       
		   cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
		   cal.set(Calendar.HOUR_OF_DAY, 0);
	       cal.set(Calendar.SECOND,0);
	       cal.set(Calendar.MINUTE,0);
	       cal.set(Calendar.MILLISECOND,0);
		  return cal.getTime();
	   }
	   /**
	    * 解析时间，若不存在，则保存dateformat
	    * @param string
	    * @param pattern
	    * @return
	    */
	public final static Date parseDate(String string, String pattern) {
		if(StringUtils.isEmpty(string)) {
			return null;
		}
		try {
			return new SimpleDateFormat(pattern).parse(string);
		} catch (Exception e) {
			System.err.println(string+"==>"+pattern);
			e.printStackTrace();
		}
		return null;
	}
	
	private final static Pattern yyyy_MM_dd_P = Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\d$");
	private final static Pattern y_M_d_H_m_s_P = Pattern.compile("^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$");
	public static Date parseDateByNormPattern(String value) {
		if(y_M_d_H_m_s_P.matcher(value).find()) {
			return parseDate(value, DATE_TIME_FORMAT);
		}else if(yyyy_MM_dd_P.matcher(value).find()) {
			return parseDate(value, YYYY_MM_DD);
		}
		return null;
	}
	
	public static void main(String[] args) {
		Date date = parseDateByNormPattern("1991-02-13");
		System.out.println(date);
	}
}
